package com.liuhouse.controller;

import com.liuhouse.dto.AddArticleDto;
import com.liuhouse.pojo.Article;
import com.liuhouse.service.IArticleService;
import com.liuhouse.utils.ResponseResult;
import com.liuhouse.vo.ArticleByIdVo;
import com.liuhouse.vo.PageVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/content/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;

    @PostMapping
    public ResponseResult add(@RequestBody AddArticleDto articleDto){
        return articleService.add(articleDto);
    }

    /**
     * 分页查询文章数据
     * 这些都是前端会传递过来 因为是后端要求的
     * @param article  文章查筛选的参数
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult list(Article article , Integer pageNum , Integer pageSize){
        PageVo pageVo = articleService.selectArticlePage(article, pageNum, pageSize);
        return ResponseResult.okResult(pageVo);
    }



    //进行文章的回显
    @GetMapping("/{id}")
    public ResponseResult getInfo(@PathVariable(value = "id") Long id){
        ArticleByIdVo info = articleService.getInfo(id);
        return ResponseResult.okResult(info);
    }


    //进行文章的修改
    @PutMapping
    public ResponseResult edit(@RequestBody AddArticleDto articleDto){
        articleService.edit(articleDto);
        return ResponseResult.okResult();
    }

    //进行文章的删除
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable(value = "id") Long id){
        //删除文章需要做两件事
        //1.删除文章的本身信息
        //2.删除文章对应的标签信息
        articleService.deleteArticleById(id);
        return ResponseResult.okResult();
    }




}
